package com.coocaa.x.app.hall.web;

import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.coocaa.x.app.hall.view.widget.TipToast;
import com.coocaa.x.framework.utils.LogUtils;

/**
 * Created by admin on 2015/6/18.
 */
public class HallWebViewClient extends WebViewClient {

    public interface WebviewClientListener {
        public void onPageStarted(WebView view, String url, Bitmap favicon);

        public void onPageFinished(WebView view, String url);
    }

    WebviewClientListener mWebviewClientListener = null;

    public HallWebViewClient(WebviewClientListener listener) {
        mWebviewClientListener = listener;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return super.shouldOverrideUrlLoading(view, url);
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        mWebviewClientListener.onPageStarted(view, url, favicon);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        mWebviewClientListener.onPageFinished(view, url);
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        LogUtils.d("web", "onReceivedError description: " + description);
//        Toast.makeText(CoocaaApplication.getContext(),"loading failed!!!",Toast.LENGTH_LONG).show();
        TipToast.getInstance().showToast("加载网页失败!");
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

    @Override
    public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
        super.doUpdateVisitedHistory(view, url, isReload);
    }

    @Override
    public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
        return super.shouldOverrideKeyEvent(view, event);
    }
}
